package com.rongshu.view.userInfo.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.CommentMeMessage;
import com.rongshu.view.userInfo.iview.ICommentMeListView;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;

/**
 * created by zhengweis on 2019/7/9.
 * description：
 */
public class CommentMeListPresenter extends BasePresenter<ICommentMeListView> {
    public CommentMeListPresenter(Context context) {
        super(context);
    }

    public void requestCommentMessage(int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getCommentMeMessage(UserInfoUtils.getUserId(), pageIndex, pageSize, Common.encryptionSign(map))
                .compose(getView().<CommentMeMessage>bindToLife())
                .subscribe(new Subscriber<CommentMeMessage>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CommentMeMessage systemMessage) {
                        if (systemMessage.getErrCode() == 0) {
                            getView().onCommentMessageList(systemMessage);
                        } else {
                            ToastUtils.show(systemMessage.getErrMsg());
                        }
                    }
                });
    }

    /**
     * 文章评论删除/回复删除
     */
    public void getCommentOrReplyDelete(int contribute_id, int comment_id, int reply_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", contribute_id);
        map.put("comment_id", comment_id);
        map.put("reply_id", reply_id);
        RetorfitService.getCommentOrReplyDelete(UserInfoUtils.getUserId(), contribute_id, comment_id, reply_id, Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {

                    }
                });
    }

    /**
     * 点击私信/@我的/文章评论消息，标记全部已读
     */
    public void GetMsgAllRead() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("type", Constant.ARTICLE_COMMENT_ME);
        RetorfitService.GetMsgAllRead(UserInfoUtils.getUserId(), Constant.ARTICLE_COMMENT_ME, Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {

                    }
                });
    }

    /**
     * 点击文章评论消息阅读
     * 不绑定生命周期，防止进入下级页面，当前页面的阅读状态无法更新
     */
    public void GetSetReadCommentMsg(int commentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("commentId", commentId);
        RetorfitService.GetSetReadCommentMsg(commentId, Common.encryptionSign(map))
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        getView().onSetReadeComment(bean);
                    }
                });
    }
}
